"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 资源池化
Case Name   : 存储过程支持自治事务
Create At   : 2023.5
Owner       : opentestcase009
Description :
    1、查看集群状态
    2、创建表,插入数据
    3、创建包含自治事务的存储过程
    4、创建调用自治事务存储过程的普通存储过程
    5、调用普通存储过程并查看表结果
    6、清理环境
Expect      :
    1、集群状态正常,数据库节点状态正常
    2、建表成功,插入数据成功
    3、创建存储过程成功
    4、创建存储过程成功
    5、查看数据成功
    6、清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.t_name = 't_dms_page_014'
        self.pro_name1 = 'p_dms_page_014_1'
        self.pro_name2 = 'p_dms_page_014_2'

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:创建表,插入数据; expect:建表成功,插入数据成功-----'
        self.log.info(text)

        sql = f"create table {self.t_name}(id int,salary int) " \
              f"with(segment=on);" \
              f"insert into {self.t_name} values(generate_series(20,50)," \
              f"generate_series(20,50));" \
              f"select count(*) from {self.t_name};"
        self.log.info(sql)
        res = self.comsh.execut_db_sql(sql)
        self.log.info(res)
        self.assertTrue(self.constant.CREATE_TABLE_SUCCESS in res
                        and self.constant.INSERT_SUCCESS_MSG in res,
                        '执行失败' + text)
        self.assertEqual('31', res.splitlines()[-2].strip(), '执行失败' + text)

        text = '-----step3:创建包含自治事务的存储过程; expect:创建成功-----'
        self.log.info(text)

        proce = f'''create or replace procedure {self.pro_name1}(a int, b int)
                as declare
                    id int := a;
                    salary int := b;
                    pragma autonomous_transaction;
                begin
                    insert into {self.t_name} values(id, salary);
                end;'''
        self.log.info(proce)
        cre_proce = self.comsh.execut_db_sql(proce)
        self.log.info(cre_proce)

        self.assertIn(self.constant.CREATE_PROCEDURE_SUCCESS_MSG, cre_proce,
                      '执行失败' + text)

        text = '-----step4:创建调用自治事务存储过程的普通存储过程; expect:创建成功-----'
        self.log.info(text)

        proce2 = f'''create or replace procedure {self.pro_name2}(a int, b int)  
                as declare
                begin
                    insert into {self.t_name} values(55, 555);
                    {self.pro_name1}(a,b);
                    commit;
                end;'''
        self.log.info(proce2)
        cre_proce2 = self.comsh.execut_db_sql(proce2)
        self.log.info(cre_proce2)

        self.assertIn(self.constant.CREATE_PROCEDURE_SUCCESS_MSG, cre_proce2,
                      '执行失败' + text)

        text = '-----step5:调用普通存储过程并查看表结果; expect:查看成功-----'
        self.log.info(text)

        call = f'''select {self.pro_name2}(66, 666);
            select count(*) from {self.t_name};'''
        self.log.info(call)
        call_res = self.comsh.execut_db_sql(call)
        self.log.info(call_res)

        self.assertEqual('33', call_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '-----step6:清理环境,删除表; expect:清理环境成功-----'
        self.log.info(text)

        clear = f'''drop table {self.t_name} cascade;
            drop procedure {self.pro_name1};
            drop procedure {self.pro_name2};'''
        self.log.info(clear)
        clear_res = self.comsh.execut_db_sql(clear)
        self.log.info(clear_res)

        self.assertIn(self.constant.DROP_TABLE_SUCCESS, clear_res,
                      '执行失败' + text)
        self.assertEqual(
            clear_res.count(self.constant.DROP_PROCEDURE_SUCCESS_MSG), 2,
            '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
